<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Architect," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="简述本文中，首先配置mysql主从复制，然后使用mycat进行读写分离，最后使用mycat来做分库分表。">
<meta name="keywords" content="Architect">
<meta property="og:type" content="article">
<meta property="og:title" content="架构之旅-集群与管理-数据-Mysql_读写分离&#x2F;主从复制&#x2F;分库分表">
<meta property="og:url" content="https://gitee.com/icmyfuture/icmyfuture/2018/03/02/iarc-232/index.html">
<meta property="og:site_name" content="icmyfuture">
<meta property="og:description" content="简述本文中，首先配置mysql主从复制，然后使用mycat进行读写分离，最后使用mycat来做分库分表。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-05-08T13:38:24.617Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="架构之旅-集群与管理-数据-Mysql_读写分离&#x2F;主从复制&#x2F;分库分表">
<meta name="twitter:description" content="简述本文中，首先配置mysql主从复制，然后使用mycat进行读写分离，最后使用mycat来做分库分表。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://gitee.com/icmyfuture/icmyfuture/2018/03/02/iarc-232/"/>





  <title>架构之旅-集群与管理-数据-Mysql_读写分离/主从复制/分库分表 | icmyfuture</title>
  














</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">icmyfuture</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">Nothing is true, everthing is permitted</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input" placeholder="search my blog...">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/icmyfuture/icmyfuture/2018/03/02/iarc-232/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="iDu">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="icmyfuture">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">架构之旅-集群与管理-数据-Mysql_读写分离/主从复制/分库分表</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-02T10:31:23+08:00">
                2018-03-02
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Technology/" itemprop="url" rel="index">
                    <span itemprop="name">Technology</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="简述"><a href="#简述" class="headerlink" title="简述"></a>简述</h1><p>本文中，首先配置mysql主从复制，然后使用mycat进行读写分离，最后使用mycat来做分库分表。<a id="more"></a>在<a href="/2018/02/05/iarc-130">数据库访问层</a>)，仅将mysql连接信息修改为mycat连接信息。</p>
<h1 id="源代码"><a href="#源代码" class="headerlink" title="源代码"></a>源代码</h1><p><a href="https://github.com/icmyfuture/iarc/tree/master/iarc-data" target="_blank" rel="noopener">iarc-data</a></p>
<h1 id="环境"><a href="#环境" class="headerlink" title="环境"></a>环境</h1><p>IDE: IntelliJ IDEA<br>JDK: 1.8.0_u162<br>Framework: spring boot, dubbo<br>Middleware: mycat<br>DB: MySql<br>WebServer: Tomcat (本地端口8088)</p>
<h1 id="开发"><a href="#开发" class="headerlink" title="开发"></a>开发</h1><h2 id="mysql主从复制"><a href="#mysql主从复制" class="headerlink" title="mysql主从复制"></a>mysql主从复制</h2><p>采用1主1从的结构</p>
<h3 id="mysql节点信息"><a href="#mysql节点信息" class="headerlink" title="mysql节点信息"></a>mysql节点信息</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">192.168.56.103 master</span><br><span class="line">192.168.56.101 slave</span><br></pre></td></tr></table></figure>
<h3 id="master配置"><a href="#master配置" class="headerlink" title="master配置"></a>master配置</h3><ol>
<li><p><code>/etc/my.cnf</code>，添加如下内容</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">server_id=1         # server_id必须保证主从不一样</span><br><span class="line">log-bin=mysql-bin   # mysql二进制日志</span><br><span class="line">binlog-do-db=ijava  # 指定只对ijava数据库进行二进制日志</span><br></pre></td></tr></table></figure>
</li>
<li><p>重启mysql(<code>systemctl restart mysql</code>)</p>
</li>
<li><p>登入mysql查看master信息(<code>$ mysql&gt; show master status;</code>)<br>看到如下内容</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">+------------------+----------+--------------+------------------+-------------------+</span><br><span class="line">| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |</span><br><span class="line">+------------------+----------+--------------+------------------+-------------------+</span><br><span class="line">| mysql-bin.000002 |      120 | ijava        |                  |                   |</span><br><span class="line">+------------------+----------+--------------+------------------+-------------------+</span><br></pre></td></tr></table></figure>
</li>
<li><p>创建同步用的mysql用户(也可忽略该步骤，直接使用mysql用户root进行登录)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ mysql&gt; CREATE USER &apos;repl&apos;@&apos;%&apos; IDENTIFIED BY &apos;repl422&apos;;</span><br><span class="line">$ mysql&gt; GRANT REPLICATION SLAVE ON *.* TO &apos;repl&apos;@&apos;%&apos;;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h3 id="slave配置"><a href="#slave配置" class="headerlink" title="slave配置"></a>slave配置</h3><ol>
<li><p><code>/etc/my.cnf</code>，添加如下内容</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">server_id=2            # server_id必须保证主从不一样</span><br><span class="line">replicate-do-db=ijava  # 指定只对ijava数据库进行主从复制</span><br></pre></td></tr></table></figure>
</li>
<li><p>slave中使用不同的uuid（虚拟机复制时uuid是一样的，主从复制会报错1593）<br>mysql中生成新的唯一uuid</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ mysql&gt; select uuid();</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>然后<code>vim /var/lib/mysql/auto.cnf</code><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[auto]</span><br><span class="line">server-uuid=***</span><br></pre></td></tr></table></figure></p>
<ol start="3">
<li>重启mysql(<code>systemctl restart mysql</code>)</li>
<li>指定master，启动并查看slave<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ mysql&gt; CHANGE MASTER TO MASTER_HOST=&apos;192.168.56.103&apos;,MASTER_USER=&apos;repl&apos;,MASTER_PASSWORD=&apos;repl422&apos;,MASTER_LOG_FILE=&apos;mysql-bin.000002&apos;,MASTER_LOG_POS=120;</span><br><span class="line">$ mysql&gt; start slave;</span><br><span class="line">$ mysql&gt; show slave status \G;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>关键信息如下<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">       Slave_IO_State: Waiting for master to send event</span><br><span class="line">          Master_Host: 192.168.56.103</span><br><span class="line">          Master_User: repl</span><br><span class="line">          Master_Port: 3306</span><br><span class="line">        Connect_Retry: 60</span><br><span class="line">      Master_Log_File: mysql-bin.000002</span><br><span class="line">  Read_Master_Log_Pos: 411</span><br><span class="line">       Relay_Log_File: mysqld-relay-bin.000002</span><br><span class="line">        Relay_Log_Pos: 574</span><br><span class="line">Relay_Master_Log_File: mysql-bin.000002</span><br><span class="line">     Slave_IO_Running: Yes</span><br><span class="line">    Slave_SQL_Running: Yes</span><br><span class="line">      Replicate_Do_DB: ijava</span><br></pre></td></tr></table></figure></p>
<h2 id="mycat读写分离"><a href="#mycat读写分离" class="headerlink" title="mycat读写分离"></a>mycat读写分离</h2><h3 id="mycat安装配置"><a href="#mycat安装配置" class="headerlink" title="mycat安装配置"></a>mycat安装配置</h3><ol>
<li>前往<a href="http://dl.mycat.io/1.6.5/" target="_blank" rel="noopener">mycat官网</a>下载1.6.5-release版本的tar.gz</li>
<li><p>解压至<code>/opt/mycat</code>并修改环境变量</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">export MYCAT_HOME=/opt/mycat</span><br><span class="line">export PATH=$MYCAT_HOME/bin:$PATH</span><br></pre></td></tr></table></figure>
</li>
<li><p>配置(<code>/opt/mycat/conf</code>目录中)<br>server.xml</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mycat:server SYSTEM &quot;server.dtd&quot;&gt;</span><br><span class="line">&lt;mycat:server xmlns:mycat=&quot;http://io.mycat/&quot;&gt;</span><br><span class="line">    &lt;user name=&quot;root&quot; defaultAccount=&quot;true&quot;&gt;</span><br><span class="line">        &lt;property name=&quot;password&quot;&gt;***&lt;/property&gt;</span><br><span class="line">        &lt;property name=&quot;schemas&quot;&gt;ijava&lt;/property&gt;</span><br><span class="line">    &lt;/user&gt;</span><br><span class="line">&lt;/mycat:server&gt;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>schema.xml<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mycat:schema SYSTEM &quot;schema.dtd&quot;&gt;</span><br><span class="line">&lt;mycat:schema xmlns:mycat=&quot;http://io.mycat/&quot;&gt;</span><br><span class="line">    &lt;schema name=&quot;ijava&quot; checkSQLschema=&quot;false&quot; sqlMaxLimit=&quot;100&quot;&gt;</span><br><span class="line">        &lt;table name=&quot;user&quot; dataNode=&quot;dn1&quot; /&gt;</span><br><span class="line">        &lt;table name=&quot;order&quot; dataNode=&quot;dn1&quot; /&gt;</span><br><span class="line">    &lt;/schema&gt;</span><br><span class="line">    &lt;dataNode name=&quot;dn1&quot; dataHost=&quot;localhost1&quot; database=&quot;ijava&quot; /&gt;</span><br><span class="line">    &lt;dataHost name=&quot;localhost1&quot; maxCon=&quot;1000&quot; minCon=&quot;10&quot; balance=&quot;0&quot;</span><br><span class="line">              writeType=&quot;0&quot; dbType=&quot;mysql&quot; dbDriver=&quot;native&quot; switchType=&quot;1&quot; slaveThreshold=&quot;100&quot;&gt;</span><br><span class="line">        &lt;heartbeat&gt;select user()&lt;/heartbeat&gt;</span><br><span class="line">        &lt;writeHost host=&quot;hostM1&quot; url=&quot;192.168.56.103:3306&quot; user=&quot;root&quot; password=&quot;***&quot;&gt;</span><br><span class="line">            &lt;readHost host=&quot;hostS2&quot; url=&quot;192.168.56.101:3306&quot; user=&quot;root&quot; password=&quot;***&quot; /&gt;</span><br><span class="line">        &lt;/writeHost&gt;</span><br><span class="line">    &lt;/dataHost&gt;</span><br><span class="line">&lt;/mycat:schema&gt;</span><br></pre></td></tr></table></figure></p>
<ol start="4">
<li>启动/停止mycat(<code>mycat start/stop</code>)</li>
<li>使用firewall-cmd开放mycat端口(8066,9066)</li>
</ol>
<h3 id="配置mysql使其忽略表大小写"><a href="#配置mysql使其忽略表大小写" class="headerlink" title="配置mysql使其忽略表大小写"></a>配置mysql使其忽略表大小写</h3><p><code>/etc/my.cnf</code>中，添加如下配置节并重启mysql<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lower_case_table_names=1</span><br></pre></td></tr></table></figure></p>
<h3 id="修改数据访问层"><a href="#修改数据访问层" class="headerlink" title="修改数据访问层"></a>修改<a href="/2018/02/05/iarc-130">数据访问层</a></h3><p>仅需修改连接字符串<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">spring.datasource.druid.ijava.url=jdbc:mysql://192.168.56.103:8066/ijava***</span><br></pre></td></tr></table></figure></p>
<h2 id="mycat分库分表"><a href="#mycat分库分表" class="headerlink" title="mycat分库分表"></a>mycat分库分表</h2><h3 id="mysql库与表"><a href="#mysql库与表" class="headerlink" title="mysql库与表"></a>mysql库与表</h3><p>新建2个库，并在其中新建表<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">CREATE SCHEMA isharding1;</span><br><span class="line">CREATE TABLE isharding1.user</span><br><span class="line">(</span><br><span class="line">  id   INT         NOT NULL PRIMARY KEY,</span><br><span class="line">  name VARCHAR(50) NOT NULL</span><br><span class="line">) ENGINE = InnoDB;</span><br><span class="line">CREATE TABLE isharding1.inbound</span><br><span class="line">(</span><br><span class="line">  id   INT         NOT NULL AUTO_INCREMENT PRIMARY KEY,</span><br><span class="line">  name VARCHAR(50) NOT NULL</span><br><span class="line">) ENGINE = InnoDB;</span><br><span class="line">CREATE SCHEMA isharding2;</span><br><span class="line">CREATE TABLE isharding2.user</span><br><span class="line">(</span><br><span class="line">  id   INT         NOT NULL PRIMARY KEY,</span><br><span class="line">  name VARCHAR(50) NOT NULL</span><br><span class="line">) ENGINE = InnoDB;</span><br><span class="line">CREATE TABLE isharding2.outbound</span><br><span class="line">(</span><br><span class="line">  id   INT         NOT NULL AUTO_INCREMENT PRIMARY KEY,</span><br><span class="line">  name VARCHAR(50) NOT NULL</span><br><span class="line">) ENGINE = InnoDB;</span><br></pre></td></tr></table></figure></p>
<h3 id="server-xml"><a href="#server-xml" class="headerlink" title="server.xml"></a>server.xml</h3><p>添加isharding schema<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;mycat:server&gt;</span><br><span class="line">    ...</span><br><span class="line">    &lt;user name=&quot;root&quot; defaultAccount=&quot;true&quot;&gt;</span><br><span class="line">        &lt;property name=&quot;password&quot;&gt;***&lt;/property&gt;</span><br><span class="line">        &lt;property name=&quot;schemas&quot;&gt;ijava,isharding&lt;/property&gt;</span><br><span class="line">    &lt;/user&gt;</span><br><span class="line">&lt;/mycat:server&gt;</span><br></pre></td></tr></table></figure></p>
<h3 id="schema-xml"><a href="#schema-xml" class="headerlink" title="schema.xml"></a>schema.xml</h3><p>user表用于展示水平切分，而inbound和outbound则用于垂直切分（不同表放在不同切片）<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;mycat:schema xmlns:mycat=&quot;http://io.mycat/&quot;&gt;</span><br><span class="line">    ...</span><br><span class="line">    &lt;schema name=&quot;isharding&quot; checkSQLschema=&quot;false&quot; sqlMaxLimit=&quot;100&quot;&gt;</span><br><span class="line">        &lt;table name=&quot;user&quot; dataNode=&quot;dn01,dn02&quot; rule=&quot;mod-long&quot; primaryKey=&quot;id&quot; autoIncrement=&quot;true&quot;/&gt;</span><br><span class="line">        &lt;table name=&quot;inbound&quot; dataNode=&quot;dn01&quot; /&gt;</span><br><span class="line">        &lt;table name=&quot;outbound&quot; dataNode=&quot;dn02&quot; /&gt;</span><br><span class="line">    &lt;/schema&gt;</span><br><span class="line">    ...</span><br><span class="line">    &lt;dataNode name=&quot;dn01&quot; dataHost=&quot;isharding&quot; database=&quot;isharding1&quot; /&gt;</span><br><span class="line">    &lt;dataNode name=&quot;dn02&quot; dataHost=&quot;isharding&quot; database=&quot;isharding2&quot; /&gt;</span><br><span class="line">    ...</span><br><span class="line">    &lt;dataHost name=&quot;isharding&quot; maxCon=&quot;1000&quot; minCon=&quot;10&quot; balance=&quot;0&quot;</span><br><span class="line">              writeType=&quot;0&quot; dbType=&quot;mysql&quot; dbDriver=&quot;native&quot; switchType=&quot;1&quot;  slaveThreshold=&quot;100&quot;&gt;</span><br><span class="line">        &lt;heartbeat&gt;select user()&lt;/heartbeat&gt;</span><br><span class="line">        &lt;writeHost host=&quot;hostM2&quot; url=&quot;192.168.56.103:3306&quot; user=&quot;root&quot; password=&quot;***&quot;&gt;</span><br><span class="line">        &lt;/writeHost&gt;</span><br><span class="line">    &lt;/dataHost&gt;</span><br><span class="line">&lt;/mycat:schema&gt;</span><br></pre></td></tr></table></figure></p>
<h3 id="rule-xml"><a href="#rule-xml" class="headerlink" title="rule.xml"></a>rule.xml</h3><p>由于仅有2个切片库，所以规则<code>mod-long</code>的<code>count</code>设置为2<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;function name=&quot;mod-long&quot; class=&quot;io.mycat.route.function.PartitionByMod&quot;&gt;</span><br><span class="line">    &lt;!-- how many data nodes --&gt;</span><br><span class="line">    &lt;property name=&quot;count&quot;&gt;2&lt;/property&gt;</span><br><span class="line">&lt;/function&gt;</span><br></pre></td></tr></table></figure></p>
<h3 id="重启mycat"><a href="#重启mycat" class="headerlink" title="重启mycat"></a>重启mycat</h3><p><code>mycat restart</code></p>
<h1 id="调试"><a href="#调试" class="headerlink" title="调试"></a>调试</h1><h2 id="读写分离与主从同步"><a href="#读写分离与主从同步" class="headerlink" title="读写分离与主从同步"></a>读写分离与主从同步</h2><ol>
<li>IntelliJ IDEA中启动数据与服务站点（DataApplication:8002, ServiceApplication:8001）</li>
<li><p>使用Postman来发起对<code>http://localhost:8001/user/insertUser</code>的请求<br>requestbody(format: json)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123;&quot;name&quot;: &quot;nothing is true&quot;&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>使用DataGrip连接mycat端口8066，验证数据是否正确插入，并连接mysql master/slave来检查数据是否已进行主从同步</p>
</li>
</ol>
<h2 id="分库分表"><a href="#分库分表" class="headerlink" title="分库分表"></a>分库分表</h2><h3 id="sql"><a href="#sql" class="headerlink" title="sql"></a>sql</h3><p>user的数据被拆分到2个不同的库中同名的表中，而inbound/outbound的数据则进入了不同库的不同表<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">USE isharding;</span><br><span class="line">INSERT INTO user (id, name) VALUES (1, &apos;ivan&apos;), (2, &apos;du&apos;), (3, &apos;eming&apos;), (4, &apos;he&apos;);</span><br><span class="line">INSERT INTO inbound (name) VALUES (&quot;inbound&quot;);</span><br><span class="line">INSERT INTO outbound (name) VALUES (&quot;outbound&quot;);</span><br></pre></td></tr></table></figure></p>
<h1 id="部署"><a href="#部署" class="headerlink" title="部署"></a>部署</h1><h2 id="部署到linux下tomcat中"><a href="#部署到linux下tomcat中" class="headerlink" title="部署到linux下tomcat中"></a>部署到linux下tomcat中</h2><p>参考<a href="/2018/02/05/iarc-130">数据访问层</a>中<code>部署</code>部分</p>
<h1 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h1><ul>
<li><a href="http://blog.csdn.net/envon123/article/details/76615059" target="_blank" rel="noopener">CentOS下mysql主从复制</a></li>
<li><a href="http://www.mycat.io/" target="_blank" rel="noopener">mycat官网</a></li>
<li><a href="https://www.jianshu.com/p/2d1a81b2dafc" target="_blank" rel="noopener">mycat简介</a></li>
<li><a href="http://blog.csdn.net/yingxiake/article/details/51695094" target="_blank" rel="noopener">mycat实践</a></li>
</ul>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Architect/" rel="tag"># Architect</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/02/08/iarc-231/" rel="next" title="架构之旅-集群与管理-数据-Redis">
                <i class="fa fa-chevron-left"></i> 架构之旅-集群与管理-数据-Redis
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/04/08/iarc-431/" rel="prev" title="架构之旅-扩展-数据-Messaging_RabbitMQ">
                架构之旅-扩展-数据-Messaging_RabbitMQ <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/uploads/avatar.jpg"
               alt="iDu" />
          <p class="site-author-name" itemprop="name">iDu</p>
           
              <p class="site-description motion-element" itemprop="description">诗酒风流品花月</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">23</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">7</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/icmyfuture" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://weibo.com/u/1865769731" target="_blank" title="Weibo">
                  
                    <i class="fa fa-fw fa-weibo"></i>
                  
                  Weibo
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#简述"><span class="nav-number">1.</span> <span class="nav-text">简述</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#源代码"><span class="nav-number">2.</span> <span class="nav-text">源代码</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#环境"><span class="nav-number">3.</span> <span class="nav-text">环境</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#开发"><span class="nav-number">4.</span> <span class="nav-text">开发</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#mysql主从复制"><span class="nav-number">4.1.</span> <span class="nav-text">mysql主从复制</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#mysql节点信息"><span class="nav-number">4.1.1.</span> <span class="nav-text">mysql节点信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#master配置"><span class="nav-number">4.1.2.</span> <span class="nav-text">master配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#slave配置"><span class="nav-number">4.1.3.</span> <span class="nav-text">slave配置</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#mycat读写分离"><span class="nav-number">4.2.</span> <span class="nav-text">mycat读写分离</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#mycat安装配置"><span class="nav-number">4.2.1.</span> <span class="nav-text">mycat安装配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#配置mysql使其忽略表大小写"><span class="nav-number">4.2.2.</span> <span class="nav-text">配置mysql使其忽略表大小写</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#修改数据访问层"><span class="nav-number">4.2.3.</span> <span class="nav-text">修改数据访问层</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#mycat分库分表"><span class="nav-number">4.3.</span> <span class="nav-text">mycat分库分表</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#mysql库与表"><span class="nav-number">4.3.1.</span> <span class="nav-text">mysql库与表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#server-xml"><span class="nav-number">4.3.2.</span> <span class="nav-text">server.xml</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#schema-xml"><span class="nav-number">4.3.3.</span> <span class="nav-text">schema.xml</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#rule-xml"><span class="nav-number">4.3.4.</span> <span class="nav-text">rule.xml</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#重启mycat"><span class="nav-number">4.3.5.</span> <span class="nav-text">重启mycat</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#调试"><span class="nav-number">5.</span> <span class="nav-text">调试</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#读写分离与主从同步"><span class="nav-number">5.1.</span> <span class="nav-text">读写分离与主从同步</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#分库分表"><span class="nav-number">5.2.</span> <span class="nav-text">分库分表</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#sql"><span class="nav-number">5.2.1.</span> <span class="nav-text">sql</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#部署"><span class="nav-number">6.</span> <span class="nav-text">部署</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#部署到linux下tomcat中"><span class="nav-number">6.1.</span> <span class="nav-text">部署到linux下tomcat中</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#参考"><span class="nav-number">7.</span> <span class="nav-text">参考</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2025</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">iDu</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'manual') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  

  

  

</body>
</html>
